// pages/msubmit/msubmit.js
import weapp from '../../components/weapp/index';

var encryp = require('../../assets/js/encryp.js')
var wxRequest = require('../../assets/js/wxRequest.js')
var common = require('../../assets/js/common.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentPage: 1, //当前页
    token: '',
    randomKey: '',
    sign: '',
    userId: '',
    authNoteArray: [], //授权单数据
    reviceNoteArray: [], //领用单数据

    showLoadMore: false,

    AuthNoteNull: true, //授权单是否为空
    receiveNull: true, //领用单是否为空

    array1: ['领用', '授权'],
    index1: 0,
    array2: ['待审批', '已通过', '打回'],
    index2: 0,
    array3: ['本周','本月'],
    index3: 0,
    searchFlag: -1,  // 查询条件

    timeType: 'week',  //按时间类型
  },
  /**
   * 获取授权申请单
   */
  getAuthNote: function(userId, authType, state, time, offset) {
    var that = this;
    var simpleObject = {
      id: userId,
      state: state,
      createTime: time,
      offset: offset,
      limit: getApp().globalData.limit
    };
    var param = common.packParam(that.data.randomKey, simpleObject);
    var url = getApp().globalData.httpUrl + '/authNote/getAuthNoteByUser';
    var info = wxRequest.postRequest(url, param, that.data.token).
    then(res => {
      var datas = res.data
      var data = {
        AuthNoteNull: that.data.AuthNoteNull,
        authNoteArray: that.data.authNoteArray,
        showLoadMore: that.data.showLoadMore
      };
      if (datas == '' || datas == []) {
        data.AuthNoteNull = true;
      } else {
        data.AuthNoteNull = false;
        for (var i = 0; i < datas.length; i++) {
          data.authNoteArray.push(datas[i])
        }
        data.showLoadMore = false;
      }
      this.setData(data)

    })
  },
  /**
   * 获取领用申请单(正常显示时)
   */
  getReviceNote: function(userId, authType, state, time, offset) {
    var that = this;
    var simpleObject = {
      id: userId,
      state: state,
      createTime: time,
      offset: offset,
      limit: getApp().globalData.limit
    };
    var param = common.packParam(that.data.randomKey, simpleObject);
    var url = getApp().globalData.httpUrl + '/reviceNote/getReviceNoteByUser';
    var info = wxRequest.postRequest(url, param, that.data.token).
    then(res => {
      var datas = res.data
      var data = {
        receiveNull: that.data.receiveNull,
        reviceNoteArray: that.data.reviceNoteArray,
        showLoadMore: that.data.showLoadMore
      };
      if (datas == '' || datas == []) {
        data.receiveNull = true;
      } else {
        data.receiveNull = false;
        for (var i = 0; i < datas.length; i++) {
          data.reviceNoteArray.push(datas[i])
        }
        data.showLoadMore = false;
      }
      this.setData(data)
    })
  },
  /**
   * 授权详情跳转
   */
  gotoInfo: function(e) {
    var that = this;
    var index = parseInt(e.currentTarget.dataset.index)
    let authInfo = JSON.stringify(that.data.authNoteArray[index]);

    wx.navigateTo({
      url: '/pages/msubmit_info/msubmit_info?authInfo=' + authInfo,
    })
  },
  /**
   * 领用详情跳转
   */
  gotoInfoR: function(e) {
    var that = this;
    var index = parseInt(e.currentTarget.dataset.index)
    console.log(index)
    let reviceInfo = JSON.stringify(that.data.reviceNoteArray[index]);

    wx.navigateTo({
      url: '/pages/msubmit_info/msubmit_info?reviceInfo=' + reviceInfo,
    })
  },
  /**
   * 显示没有更多数据
   */
  getNotMore: function(){
    var that = this;
    var flag = that.data.searchFlag
    var isNull;
    if (flag == '-1'){           //全部
      isNull = that.data.AuthNoteNull && that.data.receiveNull
    } else if (flag == '0'){     //领用
      isNull = that.data.receiveNull
    } else if (flag == '1') {    //授权
      isNull = that.data.AuthNoteNull
    } else if (flag == '3') {    //状态
      isNull = that.data.AuthNoteNull && that.data.receiveNull
    } else if (flag == '4') {    //时间
      isNull = that.data.AuthNoteNull && that.data.receiveNull
    }
    if (isNull) {
      this.oLoader.setStatus('nomore');  //显示暂无数据
      that.setData({
        showLoadMore: false   //隐藏加载更多动画
      })
    } else {
      that.setData({
        showLoadMore: false   //隐藏加载更多动画
      })
      this.oLoader.setStatus('');
    }
  },
  /**
   * 加载更多
   */
  loadMore: function() {
    var that = this;
    that.setData({
      showLoadMore: true //显示加载更多动画
    })
    //页数加1
    var page = that.data.currentPage + 1;
    that.setData({
      currentPage: page
    })
    // 获取数据 
    var index = that.data.searchFlag
    if (index=='-1'){           //全部
      that.getAuthNote(that.data.userId, '', '', '', that.data.currentPage)
      that.getReviceNote(that.data.userId, '', '', '', that.data.currentPage)
    } else if (index == '0'){   //领用
      that.getReviceNote(that.data.userId, '', '', '', that.data.currentPage)
    } else if (index == '1'){   //授权
      that.getAuthNote(that.data.userId, '', '', '', that.data.currentPage)
    }else if(index == '3'){
      that.getAuthNote(that.data.userId, '', that.data.index2, '', that.data.currentPage)
      that.getReviceNote(that.data.userId, '', that.data.index2, '', that.data.currentPage)
    } else if (index == '4') {
      that.getAuthNote(that.data.userId, '', '', that.data.timeType, that.data.currentPage)
      that.getReviceNote(that.data.userId, '', '', that.data.timeType, that.data.currentPage)
    }

    //设置无数据时的显示状态
    that.getNotMore()
  },
  // ---------------------------
  /**
   * 申请类型滚动
   */
  bindPickerChange1: function (e) {
    var that = this;
    var index = e.detail.value
    that.setData({
      index1: e.detail.value,
      currentPage: 1,
      authNoteArray: [],
      reviceNoteArray: [],
    })
    // 0为领用，1为授权
    if (index == '0'){
        that.setData({
          searchFlag: 0,
        })
      that.getReviceNote(that.data.userId, '', '', '', that.data.currentPage)
    }else{
        that.setData({
          searchFlag: 1,
        })
      that.getAuthNote(that.data.userId, '', '', '', that.data.currentPage)
    }
    this.oLoader.setStatus('');
  },
  bindPickerChange2: function(e){
    var that = this;
    var index = e.detail.value
    that.setData({
      currentPage: 1,
      searchFlag: 3,     //查找类型(状态)
      authNoteArray: [], //授权单数据
      reviceNoteArray: [], //领用单数据
    })
    if (index == 0){           //待审批
      index = 1
    } else if (index == 1) {   //已通过
      index = 3
    } else {                   //打回
      index = 4
    }
    that.setData({
      index2: index
    })
    that.getAuthNote(that.data.userId, '', that.data.index2, '', that.data.currentPage)
    that.getReviceNote(that.data.userId, '', that.data.index2, '', that.data.currentPage)
    this.oLoader.setStatus('');
  },
  bindPickerChange3: function (e) {
    var that = this;
    var index = e.detail.value
    that.setData({
      index3: e.detail.value,
      currentPage: 1,
      searchFlag: 4,     //查找类型(时间)
      authNoteArray: [], //授权单数据
      reviceNoteArray: [], //领用单数据
    })
    var timeType;
    if (index == '0'){           //本周
      timeType = 'week'
    } else if (index == '1') {   //本月
      timeType = 'mouth'
    }
    that.setData({
      timeType: timeType
    })
    that.getAuthNote(that.data.userId, '', '', timeType, that.data.currentPage)
    that.getReviceNote(that.data.userId, '', '', timeType, that.data.currentPage)
    this.oLoader.setStatus('');
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    // 每个页面都需要先获取缓存中的token和security
    var tokens = wx.getStorageSync('token')
    var randomKey = wx.getStorageSync('randomKey')
    var sign = wx.getStorageSync('sign')
    var id = wx.getStorageSync('id')
    if (tokens && randomKey) {
      that.setData({
        token: tokens,
        randomKey: randomKey,
        sign: sign,
        userId: id
      })
    }
    that.getAuthNote(that.data.userId, '', '', '', that.data.currentPage)
    that.getReviceNote(that.data.userId, '', '', '', that.data.currentPage)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    var that = this;
    this.oLoader = weapp.Loader();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})